package com.yellowpage.white.generator;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.yellowpage.white.util.DomParser;

public class WhiteGenerator {

	private Document doc;

	private static String geneType;
	
	private static List properties = new ArrayList();
	private static Map propertieTips = new HashMap();
	private static Map propertieValues = new HashMap();	
	
	private static Map generatorClasses = new HashMap();	
	private static List generators = new ArrayList();

	private static void parsingFile(Document doc) {
		Element smoothElem = doc.getDocumentElement();
		
		//generators
		Node generatorsDeclareNode = DomParser.getSubNodeByName(smoothElem, "generators");
		// generator
		List generatorDeclareNodeList = DomParser.getSubNodeListByName(generatorsDeclareNode, "generator");			
		for(int i = 0; i < generatorDeclareNodeList.size(); i++){
			Element generatorDeclareElem = (Element) generatorDeclareNodeList.get(i);
			generatorClasses.put(generatorDeclareElem.getAttribute("name"), generatorDeclareElem.getAttribute("class"));
		}

		//tools
		Node toolsNode = DomParser.getSubNodeByName(smoothElem, "tools");
		
		// type
		Node typeNode = DomParser.getSubNodeByName(toolsNode, "type");
		geneType = typeNode.getTextContent();

        // tool
		List toolNodeList = DomParser.getSubNodeListByName(toolsNode, "tool");	
		
		for(int i = 0; i < toolNodeList.size(); i++){
			Element toolElem = (Element) toolNodeList.get(i);
			
			if(geneType.equals(toolElem.getAttribute("type"))){
				//properties
				Node propertiesNode = DomParser.getSubNodeByName(toolElem, "properties");
				
				List propertyNodeList = DomParser.getSubNodeListByName(propertiesNode, "property");					
				for (int n = 0; n < propertyNodeList.size(); n++) {
					//property
					Element propertyElem = (Element) propertyNodeList.get(n);	
					
					
					if(propertyElem.hasAttribute("tips")){
						properties.add(propertyElem.getAttribute("name"));
					    propertieTips.put(propertyElem.getAttribute("name"), propertyElem.getAttribute("tips"));					    
					}else{
						propertieValues.put(propertyElem.getAttribute("name"), propertyElem.getAttribute("value"));
					}
				}
				
				//generators
				Node generatorsNode = DomParser.getSubNodeByName(toolElem, "generators");
				
				List generatorNodeList = DomParser.getSubNodeListByName(generatorsNode, "generator");					
				for (int n = 0; n < generatorNodeList.size(); n++) {
					//generator
					Element generatorElem = (Element) generatorNodeList.get(n);
					generators.add(generatorClasses.get(generatorElem.getAttribute("name")));
				}
			}	
		}			
	}

	public static void main(String[] args) {
		
		//解析Generator配置文件
		try {
			InputStream input = Thread.currentThread().getClass().getResourceAsStream("/generator.xml");
			
			parsingFile((new DomParser(input)).getDocument());

		} catch (Exception e) {						
			e.printStackTrace();
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					System.in));

			//处理用户输入
			for(int i = 0; i < properties.size(); i++) {
				String property = (String) properties.get(i);
				System.out.println(propertieTips.get(property));
				propertieValues.put(property, reader.readLine().trim());
			}
			
			//生成器生成代码
			for(int i = 0; i < generators.size(); i++) {
				Generator generator = (Generator)Class.forName((String)generators.get(i)).newInstance();
				
				generator.setProperties(propertieValues);
				generator.generate();
			}	
			
			 System.out.println("White Generator Success....");
			 System.out.println("按任意键退出....");
			
			 reader.readLine();
		} catch (Exception ex) {
			System.out.println("White Generator Failure get ERRORS :");
			ex.printStackTrace();
		}
	}

}
